package com.example.smartcity.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.Advice;
import com.example.smartcity.Service.AdviceService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AdviceManagerController {
    @Autowired
    @Qualifier(value = "AdviceService")
    private AdviceService adviceService;

    @RequestMapping(value = "/sendadvice",method = RequestMethod.POST)
    public String addAdvice(@RequestBody String adviceMap){
        System.out.println(adviceMap);
        Advice advice=new Advice();
        try {
            advice = new ObjectMapper().readValue(adviceMap, Advice.class);
        }catch (Exception e){
            System.out.println(e);
        }

        return  adviceService.AddAdvice(advice);
    }
    @RequestMapping(value = "/getalladvice",method = RequestMethod.GET)
    public String getAllAdvice(){
        List<Advice> adviceList = adviceService.findAll();
        Gson gson = new Gson();
        String jsonString = gson.toJson(adviceList);
        System.out.println(jsonString);
        JSONObject map = new JSONObject();
        map.put("code",200);
        map.put("msg","success");
        map.put("adviceList",adviceList);
        return JSONObject.toJSONString(map);
    }



}
